package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.adapter.MyRewardTasksAdapter;
import com.example.mydy.model.RewardTask;
import com.example.mydy.utils.MockDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的悬赏任务 - 用户查看已接取的悬赏任务
 */
public class MyRewardTasksActivity extends AppCompatActivity implements MyRewardTasksAdapter.OnTaskActionListener {

    private RecyclerView rvMyTasks;
    private LinearLayout emptyView;
    private Button btnBack;
    private MyRewardTasksAdapter adapter;
    private MockDataManager mockDataManager;
    private String currentUsername;
    private List<RewardTask> myTasks = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_reward_tasks);

        // 初始化数据管理器
        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUser().getUsername();

        // 初始化视图
        initViews();
        
        // 设置点击事件
        setupClickListeners();
        
        // 加载我的任务数据
        loadMyTasks();
    }

    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        rvMyTasks = findViewById(R.id.rv_my_tasks);
        emptyView = findViewById(R.id.empty_view);
        
        // 设置RecyclerView
        rvMyTasks.setLayoutManager(new LinearLayoutManager(this));
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());
    }

    private void loadMyTasks() {
        // 获取用户接取的任务
        myTasks = MockDataManager.getUserRewardTasks(currentUsername);
        
        // 检查是否有数据
        if (myTasks.isEmpty()) {
            rvMyTasks.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        } else {
            rvMyTasks.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            
            // 设置适配器
            adapter = new MyRewardTasksAdapter(this, myTasks, this);
            rvMyTasks.setAdapter(adapter);
        }
    }

    @Override
    public void onTaskSelected(RewardTask task) {
        // 查看任务详情
        Toast.makeText(this, "查看任务: " + task.getTitle(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTaskSubmit(RewardTask task) {
        // 跳转到提交任务页面
        Intent intent = new Intent(this, SubmitRewardTaskActivity.class);
        intent.putExtra("task_id", task.getId());
        startActivity(intent);
    }

    @Override
    public void onTaskCancel(RewardTask task) {
        // 取消任务
        boolean success = mockDataManager.cancelRewardTask(currentUsername, task.getId());
        if (success) {
            Toast.makeText(this, "成功取消任务", Toast.LENGTH_SHORT).show();
            // 刷新数据
            loadMyTasks();
        } else {
            Toast.makeText(this, "取消任务失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 每次恢复时刷新列表，以显示最新状态
        loadMyTasks();
    }
} 